import { Switch } from 'antd';
import React, { useState } from 'react';
import { Type } from '../../../../features/util/dictionary';

interface Props {
    /**开关控件的默认值 */
    value?: boolean

    /**用于触发表单值的更新 */
    onChange?: Function

    /**开关为真时的展示文本 */
    onText?: string

    /**开关为假时的展示文本 */
    offText?: string

    /**开关显示文本的对象 */
    dictionary?: Type[]
}

const SimpleSwitch: React.FC<Props> = ({
    value,
    onChange,
    onText,
    offText,
    dictionary,
}) => {

    const [checkedChildren] = useState(() => onText || dictionary?.find((item: Type) => item.value)?.name || '开启')
    const [unCheckedChildren] = useState(() => offText || dictionary?.find((item: Type) => !item.value)?.name || '关闭')

    function handleChange(checked: boolean) {
        onChange && onChange(checked)
    }

    return (
        <Switch
            checkedChildren={checkedChildren}
            unCheckedChildren={unCheckedChildren}
            onChange={handleChange}
            checked={value}
        />
    )
}

export default SimpleSwitch;